//
//  HTTPHelper+Emp.m
//  SalonManage
//
//  Created by wulongmin on 17/7/12.
//  Copyright © 2017年 lmtt. All rights reserved.
//

#import "HTTPHelper+Emp.h"
#import "EmpModel.h"

@implementation HTTPHelper (Emp)

- (NSURLSessionDataTask *)checkUnitId:(NSString *)unit_id
                            stationNo:(NSString *)stationno
                              success:(void(^)())success {
    
    NSString *url = [API_BASE_URL_STRING stringByAppendingString:@"base/check"];
    NSMutableDictionary *params = [NSMutableDictionary dictionary];
    params[@"unit_id"] = unit_id;
    params[@"stationno"] = stationno;
    return [self get:url params:params useToken:NO showText:@"检验中" success:^(NSURLSessionDataTask * _Nonnull task, NSDictionary * _Nullable result, HTTPStatus * _Nonnull status) {
        
        if ([status.code isEqualToString:KF_HTTP_OK]) {
            success();
        }else {
            [MBProgressHUD showText:status.msg onView:nil];
            NSLog(@"%@",status);
        }
        
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        
    }];
}

- (NSURLSessionDataTask *)registerWithUnitName:(NSString *)unit_name
                                   devSerialNo:(NSString *)dev_serialNo
                                       devName:(NSString *)dev_name
                                       custNum:(NSString *)cust_num
                                       success:(void(^)())success {
    
    
    NSString *url = [API_EMP stringByAppendingString:@"register"];
    NSMutableDictionary *params = [NSMutableDictionary dictionary];
    params[@"unit_name"] = unit_name;
    params[@"dev_serialno"] = dev_serialNo;
    params[@"dev_name"] = dev_name;
    params[@"cust_num"] = cust_num;
    
    return [self post:url params:params useToken:YES showText:@"通信中" success:^(NSURLSessionDataTask * _Nonnull task, NSDictionary * _Nullable result, HTTPStatus * _Nonnull status) {
        
        if ([status.code isEqualToString:KF_HTTP_OK]) {
            
            
            
            success();
            
        }else {
            [MBProgressHUD showText:status.msg onView:nil];
            NSLog(@"%@",status);
        }
        
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        
    }];
    
}

- (NSURLSessionDataTask *)changePwdWithCustNum:(NSString *)cust_num
                                        newPwd:(NSString *)newPwd
                                        oldPwd:(NSString *)oldPwd
                                       success:(void(^)())success {
    
    NSString *url = [API_EMP stringByAppendingString:@"changepwd"];
    NSMutableDictionary *params = [NSMutableDictionary dictionary];
    params[@"cust_num"] = cust_num;
    params[@"password"] = newPwd;
    params[@"old_password"] = oldPwd;
    
    return [self post:url params:params useToken:YES showText:@"校验中" success:^(NSURLSessionDataTask * _Nonnull task, NSDictionary * _Nullable result, HTTPStatus * _Nonnull status) {
        
        if ([status.code isEqualToString:KF_HTTP_OK]) {
            
            success();
        }else {
            [MBProgressHUD showText:status.msg onView:nil];
            NSLog(@"%@",status);
        }
        
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        
    }];
}

- (NSURLSessionDataTask *)loginWithCustNum:(NSString *)cust_num
                                       pwd:(NSString *)pwd
                               devSerialNo:(NSString *)dev_serialno
                                   devName:(NSString *)dev_name
                                   success:(void(^)(EmpModel *emp))success {
    
    NSString *url = [API_EMP stringByAppendingString:@"login"];
    NSMutableDictionary *params = [NSMutableDictionary dictionary];
    params[@"cust_num"] = cust_num;
    params[@"password"] = pwd;
    params[@"dev_serialno"] = dev_serialno;
    params[@"dev_name"] = dev_name;
    
    return [self post:url params:params useToken:YES showText:@"通信中" success:^(NSURLSessionDataTask * _Nonnull task, NSDictionary * _Nullable result, HTTPStatus * _Nonnull status) {
        
        if ([status.code isEqualToString:KF_HTTP_OK]) {
            EmpModel *emp = [EmpModel mj_objectWithKeyValues:result];
            [UserDefaultsHelper setString:cust_num key:KF_CUST_NUM];
            [UserDefaultsHelper setString:emp.cust_name key:KF_CUST_NAME];
            success(emp);
        }else {
            [MBProgressHUD showText:status.msg onView:nil];
            NSLog(@"%@",status);
        }
        
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        
    }];
}

- (NSURLSessionDataTask *)getEmpDataWithUnitId:(NSString *)unit_id
                                     stationNo:(NSString *)stationno
                                       custNum:(NSString *)cust_num
                                       success:(void(^)(EmpModel *emp))success
                                          fail:(void(^)())fail {
    
    NSString *url = [API_EMP stringByAppendingString:@"data"];
    NSMutableDictionary *params = [NSMutableDictionary dictionary];
    params[@"unit_id"] = unit_id;
    params[@"stationno"] = stationno;
    params[@"cust_num"] = cust_num;
    
    return [self post:url params:params useToken:NO showText:@"通信中" success:^(NSURLSessionDataTask * _Nonnull task, NSDictionary * _Nullable result, HTTPStatus * _Nonnull status) {
        
        if ([status.code isEqualToString:KF_HTTP_OK]) {
            EmpModel *emp = [EmpModel mj_objectWithKeyValues:result];
            success(emp);
        }else {
            
            fail();
            NSLog(@"%@",status);
        }
        
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        
    }];
}

- (void)logout {
    
    [UserDefaultsHelper setString:nil key:KF_CUST_NUM];
    [UserDefaultsHelper setString:nil key:KF_CUST_NAME];
    [UserDefaultsHelper setString:nil key:KF_UNIT_ID];
    [UserDefaultsHelper setString:nil key:KF_STATION_NO];
}

@end
